package com.irs.irsforum.web;


import com.irs.irsforum.entity.Message;
import com.irs.irsforum.security.access.Access;
import com.irs.irsforum.security.access.UserType;
import com.irs.irsforum.service.IMessageService;
import com.irs.irsforum.web.common.BaseController;
import com.irs.irsforum.web.common.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yihuili
 * @since 2020-02-12
 */
@RestController
@RequestMapping("/message")
public class MessageController extends BaseController {
    private static final Logger logger =
            LoggerFactory.getLogger(MessageController.class);

    @Autowired
    IMessageService iMessageService;

    /**
     * 获取消息列表
     * @param userId
     * @return
     */
    @Access(userType = {UserType.LOGIN_USER})
    @RequestMapping(value = "/mymsg", method = RequestMethod.GET)
    public Object myMessage(@ModelAttribute(OBJECT_USER_ATTRIBUTE) Long userId) {
        logger.info("userId={}获取消息列表",userId);
        List<Message> messageList = iMessageService.getMessageList(userId);
        return ResponseResult.newSuccessResult(messageList);
    }

    /**
     * 设置消息为已读
     * @param messageId
     * @param userId
     * @return
     */
    @Access(userType = {UserType.LOGIN_USER})
    @RequestMapping(value = "/setread", method = RequestMethod.POST)
    public Object setRead(@RequestParam(value = "messageId") Long messageId,
                          @ModelAttribute(OBJECT_USER_ATTRIBUTE) Long userId){
        logger.info("设置消息={}为已读",messageId);
        Integer result = iMessageService.setRead(userId, messageId);
        if(result == 0){
            logger.error("设置消息已读失败");
            return ResponseResult.newFailResult("设置消息已读失败");
        }
        else{
            return ResponseResult.newSuccessResult();
        }
    }

    /**
     * 删除消息
     * @param messageId
     * @param userId
     * @return
     */
    @Access(userType = {UserType.LOGIN_USER})
    @RequestMapping(value = "/delmsg", method = RequestMethod.DELETE)
    public Object delMessage(@RequestParam(value = "messageId") Long messageId,
                          @ModelAttribute(OBJECT_USER_ATTRIBUTE) Long userId){
        logger.info("删除消息={}",messageId);
        Integer result = iMessageService.delMessage(userId, messageId);
        if(result == 0){
            logger.error("删除消息失败");
            return ResponseResult.newFailResult("删除消息失败");
        }
        else{
            return ResponseResult.newSuccessResult();
        }
    }

}

